#include "mainwindow.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QScreen>
#include <QRect>

Mainwindow::Mainwindow(QWidget *parent)
	: QWidget{parent}
{
	this->setWindowTitle("计时器");

	// 设置窗口大小
	// 获取屏幕分辨率
	QScreen * screen = QGuiApplication::primaryScreen(); // 获取主屏幕
	QRect rect = screen->geometry();
	int height = rect.height(); // 获取屏幕的高度
	this->setFixedSize(height/2 + 30, height/2);
#ifdef QT_DEBUG
	qDebug() << "屏幕的高度：" << rect.size().height() << " 屏幕的宽度" << rect.size().width();
#endif

	TimerWidget = new QStackedWidget(this);
	// 初始化各个窗口
	ChronographWidget = new Timer::Chronograph(); // 秒表
	CountdownWidget = new Timer::Countdown();     // 倒计时

	TimerWidget->addWidget(ChronographWidget);
	TimerWidget->addWidget(CountdownWidget);

	// 倒计时结束时切换到对应的页面
	connect(CountdownWidget, &Timer::Countdown::EndofCountdown, this, &Mainwindow::ToCountDownWidget);

	// 按键布局
	QHBoxLayout * buttonLayout =new QHBoxLayout();
	btnGroup = new QButtonGroup();
	QPushButton * btn1 = new QPushButton("秒表");
	QPushButton * btn2 = new QPushButton("倒计时");
	btn1->setCheckable(true);
	btn2->setCheckable(true);

	buttonLayout->addWidget(btn1);
	buttonLayout->addWidget(btn2);

	btnGroup->addButton(btn1,0);
	btnGroup->addButton(btn2,1);

	// 将按钮和窗口联系起来
	connect(btn1,&QPushButton::clicked, this, &Mainwindow::Button_clicked);
	connect(btn2,&QPushButton::clicked, this, &Mainwindow::Button_clicked);

	QVBoxLayout * mainlayout = new QVBoxLayout();
	mainlayout->setSpacing(0);
	mainlayout->addWidget(TimerWidget);
	mainlayout->addLayout(buttonLayout);

	this->setLayout(mainlayout);
}

void Mainwindow::Button_clicked()
{
	int id = btnGroup->checkedId();
	TimerWidget->setCurrentIndex(id);
}

void Mainwindow::ToCountDownWidget()
{
	TimerWidget->setCurrentWidget(CountdownWidget);
}
